﻿using Sidvall.UI.Dialogs;
using System;
using System.Threading.Tasks;
using Windows.UI.Xaml.Controls;

namespace Sidvall.UI.Uwp.Dialogs
{
    public sealed partial class ProgressDialog
    {
        #region Public Members

        #region ShowAsync

        public async Task ShowAsync(ProgressDialogContext context, object owner)
        {
            var page = owner as Windows.UI.Xaml.Controls.Page;
            ControlManager.SetHeight(this, page.ActualHeight, 350);
            ControlManager.SetWidth(this, page.ActualWidth, 450);
            this.ProgressControl.InitializeContext(context);
            this.Title = context.Title;
            await ShowAsync();
        }

        #endregion

        #endregion
        #region Events

        #region ContentDialog_PrimaryButtonClick

        private async void ContentDialog_PrimaryButtonClick(Windows.UI.Xaml.Controls.ContentDialog sender, Windows.UI.Xaml.Controls.ContentDialogButtonClickEventArgs args)
        {
            args.Cancel = true;
            await this.ProgressControl.DoPrimaryActionAsync();
        }

        #endregion
        #region ProgressControl_ActivityCompleted

        private void ProgressControl_ActivityCompleted(object sender, EventArgs e)
        {
            Hide();
        }

        #endregion
        #region ProgressDialog_Closed

        private void ProgressDialog_Closed(ContentDialog sender, ContentDialogClosedEventArgs args)
        {
            this.ProgressControl.Stop();
        }

        #endregion
        #region ProgressDialog_Opened

        private async void ProgressDialog_Opened(ContentDialog sender, ContentDialogOpenedEventArgs args)
        {
            await this.ProgressControl.StartAsync();
        }

        #endregion

        #endregion
        #region Constructors

        public ProgressDialog()
        {
            this.InitializeComponent();

            this.Closed += ProgressDialog_Closed;
            this.Opened += ProgressDialog_Opened;
        }

        #endregion
    }
}
